# -*- coding: utf-8 -*-

# Scrapy settings for DataSpider project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html
import os

BOT_NAME = 'DataSpider'

SPIDER_MODULES = ['DataSpider.spiders']
NEWSPIDER_MODULE = 'DataSpider.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'DataSpider (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# 下载延迟
# DOWNLOAD_DELAY = 3

# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Cookies是否禁用 知乎要求开启Cookies才可以登录访问
# Disable cookies (enabled by default)
COOKIES_ENABLED = False
# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en',
    'Accept-Encoding': 'gzip, deflate, br',
    "Connection": "keep - alive",
    "Cookie": '__utma=253067679.976195316.1584010343.1584015061.1584021661.3; __utmc=253067679; __utmz=253067679.1584010343.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; _ga=GA1.2.976195316.1584010343; _gid=GA1.2.392339473.1584015118; __utmb=253067679.16.10.1584021661; session=fd5e9fc5-b72e-4497-88e2-83a9bd9e0896; __utmv=253067679.|1=Is%20Registered=No=1^3=ad_block=No=1; __utmt=1',
}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'DataSpider.middlewares.FirtscrapySpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    'DataSpider.middlewares.RandomUserAgentMiddleWare': 2,
    'scrapy.downloadermiddleware.useragent.UserAgentMiddleware': None,
    # 'DataSpider.middlewares.RandomProxyMiddleware': 1,
    # 通过selenium来访问动态页面
    # 'DataSpider.middlewares.JSPageMiddleware': 3,
}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    # 'DataSpider.pipelines.JsonItemExporterPipeline': 300,
    'DataSpider.pipelines.ArticleImagesPipline': 200,
    # 'scrapy.pipelines.images.ImagesPipeline':100,
    # 'DataSpider.pipelines.MysqlTwistedPipeline': 250,
    'DataSpider.pipelines.ElasticsearchPipline': 100,

}
IMAGES_URLS_FIELD = "front_image_url"
project_dir = os.path.abspath(os.path.dirname(__file__))
IMAGES_STORE = os.path.join(project_dir, "images")

# IMAGES_MIN_WIDTH = 100
# IMAGES_MIN_HEIGHT = 100

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
# 下载速度控制算法
# AUTOTHROTTLE_ENABLED = False
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = T
RANDOM_UA_TYPE = "random"
MYSQL_HOST = "127.0.0.1"
MYSQL_DBNAME = "search_spider"
MYSQL_USER = "root"
MYSQL_PASSWD = "123456"
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

# MEDIA_ALLOW_REDIRECTS =True
# REDIRECT_ENABLED = False                       # 关掉重定向, 不会重定向到新的地址
# HTTPERROR_ALLOWED_CODES = [301, 302]     # 返回301, 302时, 按正常返回对待, 可以正常写入cookie

SQL_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
